package com.zyl.demo.controller;

import com.zyl.demo.beans.BaseBean;
import com.zyl.demo.beans.Commodity;
import com.zyl.demo.beans.Home;
import com.zyl.demo.service.CommodityService;
import com.zyl.demo.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HomeController {
    @Autowired
    HomeService homeService;
    @Autowired
    CommodityService commodityService;

    @PostMapping("/getHomePageCommodity")
    public BaseBean getHomePageCommodity() {
        List<Home> allHome = homeService.getAllHome();
        List<Commodity> homeCommoditys = new ArrayList<>();
        allHome.forEach(v -> {
            homeCommoditys.add(commodityService.getCommodityById(v.getCommodityId()));
        });
        return new BaseBean(homeCommoditys);
    }

    @PostMapping("/getCommodityPage")
    public BaseBean getCommodityPage(String page) {
        Integer pageNme = Integer.valueOf(page);
        if (pageNme<1){
            pageNme=1;
        }
        List<Commodity> commodityPageList = commodityService.getCommodityPage((pageNme-1)*10, 10);
        return new BaseBean(commodityPageList);
    }



}
